class ResponsesController < ApplicationController
  before_filter :approval_required, :only => [ :new, :create, :edit, :update ]
  before_filter :load_argument  

  def load_argument
    @argument = Argument.find(params[:argument_id])
  end
  
  # GET /responses
  # GET /responses.xml
  def index
    @responses = @argument.responses.find(:all)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @responses }
    end
  end

  # GET /responses/1
  # GET /responses/1.xml
  def show
    @response = @argument.responses.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => response }
    end
  end
  
  # GET /responses/new
  # GET /responses/new.xml
  def new
    @response = @argument.responses.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => response }
    end
  end

  # GET /responses/1/edit
  def edit
    @response = @argument.responses.find(params[:id])
  end

  # POST /responses
  # POST /responses.xml
  def create
    @response = @argument.responses.build(params[:response])

    respond_to do |format|
      if @response.save
        flash[:notice] = 'Response was successfully created.'.t
        format.html { redirect_to(@argument) }
        format.xml  { render :xml => response, :status => :created, :location => response }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => response.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /responses/1
  # PUT /responses/1.xml
  def update
    # only allows you to edit your own records
    user_id = current_user.id || nil
    @response = @argument.responses.find(params[:id], :conditions => [ "user_id = ?", user_id])

    respond_to do |format|
      if @response.update_attributes(params[:response])
        flash[:notice] = 'Response was successfully updated.'.t
        format.html { redirect_to(@argument) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => response.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /responses/1
  # DELETE /responses/1.xml
  def destroy
    # only allows you to delete your own records
    user_id = current_user.id || nil
    @response = @argument.responses.find(params[:id], :conditions => [ "user_id = ?", user_id])
    @response.destroy

    respond_to do |format|
      format.html { redirect_to(argument_responses_url(@argument)) }
      format.xml  { head :ok }
    end
  end
end
